﻿using DataServer.ApplicationLib.Services;
using DataServer.CommonLib.Constants;
using DataServer.CommonLib.Models.DataSource;
using Microsoft.AspNetCore.Mvc;

namespace Config.WebApi.Controllers
{
    [ApiController]
    public class DataSourceController : ControllerBase
    {
        private readonly IDataSourceService _dataSourceService;

        public DataSourceController(IDataSourceService dataSourceService)
        {
            _dataSourceService = dataSourceService;
        }

        // GET: api/<DataSourceController>
        [HttpGet(ApiRoutes.DataSource.QueryDataSources)]
        public async Task<IActionResult> Get([FromQuery] DataSourceSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _dataSourceService.QueryDataSourcesByPageAsync(model);
                return Ok(data);
            }
            else
            {
                var data = await _dataSourceService.QueryDataSourcesAsync(model);
                return Ok(data);
            }
        }

        // GET api/<DataSourceController>/5
        [HttpGet(ApiRoutes.DataSource.GetDataSource)]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<DataSourceController>
        [HttpPost(ApiRoutes.DataSource.CreateDataSource)]
        public async Task<IActionResult> Post([FromBody] DataSourceEditRequestDto model)
        {
            var data = await _dataSourceService.CreateDataSourceAsync(model);
            return Ok(data);
        }

        // PUT api/<DataSourceController>/5
        [HttpPut(ApiRoutes.DataSource.UpdateDataSource)]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<DataSourceController>/5
        [HttpDelete(ApiRoutes.DataSource.DeleteDataSource)]
        public void Delete(int id)
        {
        }

        // [HttpGet("{id}/fields")]
        // public async Task<IActionResult> QueryFields(Guid id)
        // {
        //     var data = await _mediator.Send(new SearchDataSourceFieldsQuery(id));
        //     return Ok(data);
        // }
    }
}